<?php
//首页显示
namespace Home\Controller;
use Think\Controller;
class ListController extends CommonController {
    public function index(){
    	$id=I('get.id');
    	$ticket_type=M('ticket_type');
    	$brand=M('brand');
    	$shop_type=M('shop_type');
    	$ticket_types=$ticket_type->find($id);
    	$this->assign('ticket_types',$ticket_types);
    	//筛选价格
    	$shop_type_info=$shop_type->find($ticket_types['type_id']);
    	$shop_type_info['price'] = str_replace(" ","",$shop_type_info['price']);
    	$price_array=explode(',',$shop_type_info['price']);
    	$this->assign('price_array',$price_array);
    
    	//面包屑生成
    	$bids=explode(',', $ticket_types['bids']);
    	$bread[0]['href']='/';
    	$bread[0]['name']='首页';
    	foreach ($bids as $key => $value) {
    		if($key>0){
    			$type=$ticket_type->find($value);
    			$bread[$key]['href']=U('list/index',array('id' =>$value));
    			$bread[$key]['name']=$type['name'];
    		}
    	}
    	$this->assign('bread',$bread);
    	//筛选属性生成
    	$ticket_config=M('ticket_config');
    	$ticket_atr=$ticket_config->where('ishidden=0 and type_id='.$ticket_types['type_id'])->select();
    	foreach ($ticket_atr as $key => $value) {
    		$ticket_atr[$key]['content']=explode(',',$value['content']);
    	}
    	$this->assign('ticket_atr',$ticket_atr);
    	//默认条件
    	$where['_string']='FIND_IN_SET('.$id.',type_id)';
	    $where['ishidden']=0;
	    //$where['tuanselect']=array('neq',1);
	    $where_lianhe='shop.ishidden=0 and FIND_IN_SET('.$id.',shop.type_id)';//联合查询条件
	    //清空筛选

	    if(I('get.delsearchall')==1){
    		session('shop_'.$id,null);
    		session('bshop_'.$id,null);
    		session('pshop_'.$id,null);
    	}
    	//用户筛选
    	$search=I('get.search');
    	$search=urldecode($search);
    	//删除筛选
    	$delsearch=I('get.delsearch');
    	if($delsearch!=null){
    		unset($_SESSION['shop_'.$id]['shop_'.$id.'_'.$delsearch]); 
		}
    	//添加筛选条件
    	if($search){
    		$search_key=explode('_',$search);
    		$_SESSION['shop_'.$id]['shop_'.$id.'_'.$search_key[0]]=$search_key[1];
    	}
		if($_SESSION['shop_'.$id]){
			$search_array=$_SESSION['shop_'.$id];
			$where['_string']='FIND_IN_SET('.$id.',type_id) ';
    	    $where['ishidden']=0;
    	    $where_lianhe='shop.ishidden=0 and FIND_IN_SET('.$id.',shop.type_id) ';
    	    $keys='';
    		foreach ($search_array as $key => $value) {
    			$where['_string'].=' and FIND_IN_SET("'.$value.'",search_tag) ';
    			$where_lianhe.=' and FIND_IN_SET("'.$value.'",shop.search_tag) ';
    			$key=explode('_',$key);
    			$keys.=$key[2].',';
    			$new_search_array[$key[2]]=$value;
    		}
    		 $keys=explode(',',$keys);
		}
		$this->assign('keys',$keys);
   		$this->assign('new_search_array',$new_search_array);
   		//清除品牌筛选
   		if(I('get.delbrand')){
   			unset($_SESSION['bshop_'.$id]['brand_id']); 
   		}
   		//品牌筛选
   		if(I('get.brand')){
   			$_SESSION['bshop_'.$id]['brand_id']=I('get.brand');
   		}

   		if($_SESSION['bshop_'.$id]['brand_id']){
   			$where['_string'].=' and brand_id='.$_SESSION['bshop_'.$id]['brand_id'];
   			$where_lianhe.=' and shop.brand_id='.$_SESSION['bshop_'.$id]['brand_id'];
   			//品牌筛选标识
   			$brand_sx=$brand->find($_SESSION['bshop_'.$id]['brand_id']);
   			$this->assign('brand_sx',$brand_sx);
		}

		//清除价格筛选
   		if(I('get.delprice')){
   			unset($_SESSION['pshop_'.$id]['price']); 
   		}
   		//价格筛选
   		if(I('get.price')){
			$price=explode('-',I('get.price'));
   			$_SESSION['pshop_'.$id]['price']['max_price']=$price[1];
   			$_SESSION['pshop_'.$id]['price']['min_price']=$price[0];
   		}

   		if(I('get.price_min')&&I('get.price_max')){
   			$_SESSION['pshop_'.$id]['price']['max_price']=I('get.price_max');
   			$_SESSION['pshop_'.$id]['price']['min_price']=I('get.price_min');
   		}

   		if($_SESSION['pshop_'.$id]['price']['max_price']!=null&&$_SESSION['pshop_'.$id]['price']['min_price']!=null){
   			$where['_string'].=' and price>='.$_SESSION['pshop_'.$id]['price']['min_price'];
   			$where['_string'].=' and price<='.$_SESSION['pshop_'.$id]['price']['max_price'];
   			$where_lianhe.=' and shop.price>='.$_SESSION['pshop_'.$id]['price']['min_price'];
   			$where_lianhe.=' and shop.price<='.$_SESSION['pshop_'.$id]['price']['max_price'];
   			
   			//价格筛选标识
   			$price_sx=$_SESSION['pshop_'.$id]['price']['min_price'].'-'.$_SESSION['pshop_'.$id]['price']['max_price'];
   			$this->assign('price_sx',$price_sx);
		}
   		
   		//排序
   		$order=I('get.order',0);
   		$this->assign('order',$order);
   		$orders=array(
   			'0' => 'id desc', 
   			'1' => 'sale_num desc', 
   			'2' => 'price desc', 
   		);
    	//商品
    	$shop=M('shop');
    	$count      = $shop->where($where)->count();
    	$this->assign('count',$count);
	    $Page       = new \Think\Page($count,12);// 实例化分页类 传入总记录数
	    $count_page=ceil($count/12);
	    $this->assign('count_page',$count_page);
	    $Page->setConfig('header','条记录');
	    $Page->setConfig('prev','上一页');
	    $Page->setConfig('next','下一页');
	    $show       = $Page->show();// 分页显示输出
    	$shop_list	= M()
    	              ->field('shop.*,stroe.title as store_title,stroe.id as store_id')
    				  ->table(' __SHOP__ AS shop ')
    				  ->join(' LEFT JOIN __STORE__ AS stroe ON ( stroe.id = shop.store_id ) ')
    	              ->where($where_lianhe)
    	              ->order($orders[$order])
    	              ->limit($Page->firstRow.','.$Page->listRows)
    	              ->select();
    
    	foreach ($shop_list as $key => $value) {
    		$shop_list[$key]['shop_sku']=json_decode($value['shop_sku'],true);
    	}
    	$this->assign('page',$show);// 赋值分页输出
        $this->assign('shop_list',$shop_list);
        //销量排行
        $top_sale=$shop->where('ishidden=0')->order('sale_num desc')->limit(4)->select();
        $this->assign('top_sale',$top_sale);
        //获取品牌
        $bindBrand=$ticket_types['bindBrand'];
       
        $brand_array=$brand->where('id in ('.$bindBrand.')')->select();
        $this->assign('brand_array',$brand_array);
      	$this->display();
	}
	
	function group_buy(){
		//默认条件
		$time=time();
    	$where['ishidden']=0;
	    $where['tuanselect']=array('eq',1);
		$where['tuanz_endtime']=array('gt',$time);
	    $shop=M('shop');
    	$count      = $shop->where($where)->count();
    	$this->assign('count',$count);
	    $Page       = new \Think\Page($count,9);// 实例化分页类 传入总记录数
	    $count_page=ceil($count/9);
	    $this->assign('count_page',$count_page);
	    $Page->setConfig('header','条记录');
	    $Page->setConfig('prev','上一页');
	    $Page->setConfig('next','下一页');
	    $show       = $Page->show();// 分页显示输出
    	$shop_list	= $shop->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
    	$this->assign('page',$show);// 赋值分页输出
        $this->assign('shop_list',$shop_list);
        $this->display();
	}
}
?>
